package io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 *@Description:<p></p><br/>
 *@author 王旭
 *@time 2016年3月17日 下午9:44:57
 */
public class TimeServerHandler implements Runnable {
	
	public CountDownLatch latch;
	
	public AsynchronousServerSocketChannel serverSocketChannel;
	
	public TimeServerHandler(int port) {
		
		try {
			this.serverSocketChannel = AsynchronousServerSocketChannel.open();
			this.serverSocketChannel.bind(new InetSocketAddress(port));
			System.out.println("服务器已开启，监听端口：" + port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		this.latch = new CountDownLatch(1);
		
		this.serverSocketChannel.accept(this, new AcceptCompletionHandler());
		
		try {
			this.latch.await(); //等待任务处理线程完成
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
}
